package com.useeinfo.oa.modules.inspection.thread.task;

import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.inspection.biz.InspectionExecuteTimeBiz;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.inspection.entity.InspectionExecuteTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class InspectionPlanTask implements Runnable{
    @Autowired
    private InspectionExecuteTimeBiz inspectionExecuteTimeBiz;

    private final static Logger logger = LoggerFactory.getLogger(InspectionPlanTask.class);

    private volatile InspectionOrderBiz inspectionOrderBiz;

    private List<InspectionExecuteTime> inspectionExecuteTimes;

    public InspectionPlanTask(InspectionOrderBiz inspectionOrderBiz, List<InspectionExecuteTime> inspectionExecuteTimes) {
        this.inspectionOrderBiz = inspectionOrderBiz;
        this.inspectionExecuteTimes = inspectionExecuteTimes;
    }

    @Override
    public void run() {
        try {
            //TODO,在此调用计划生成工单，开始时间结束时间和提醒时间请查看planExecuteTime
            inspectionExecuteTimes.forEach(inspectionExecuteTime -> {
                Date StartDate = StringConverters.ToDatePattern(inspectionExecuteTime.getStartTime(), "yyyy-MM-dd HH:mm");
                Date endDate = StringConverters.ToDatePattern(inspectionExecuteTime.getEndTime(), "yyyy-MM-dd HH:mm");
                inspectionOrderBiz.makeInspectionOrder(inspectionExecuteTime.getPlannerInspection(), StartDate, endDate,1,inspectionExecuteTime);
            });
        } catch (Exception e) {
            logger.info("计划任务线程失败");
        }
    }
}
